package com.bm.specialvehiclefordispatcher.activity.login;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.SMSBroadcastReceiver;
import com.bm.specialvehiclefordispatcher.Tools.SMSBroadcastReceiver.MessageListener;
import com.bm.specialvehiclefordispatcher.Tools.Tools;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;

public class ForgetPasswordActivity extends BaseActivity implements
		OnClickListener {

	EditText tel;
	EditText psw;
	Button getvcode;
	Button next;
	CommonNavigationBar bar;
	LoginManager loginManager;
	/**
	 * 倒计时
	 */
	private DownTimer timer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgetpassword);
		findViews();
		init();
		addListeners();
		ToastMgr.show("");
	}

	@Override
	public void findViews() {
		bar = (CommonNavigationBar) findViewById(R.id.navi);
		tel = (EditText) findViewById(R.id.forgetpsw_tel);
		psw = (EditText) findViewById(R.id.forgetpsw_vcode);
		getvcode = (Button) findViewById(R.id.forgetpsw_get_vcode);
		next = (Button) findViewById(R.id.forgetpsw_next);
	}

	@Override
	public void init() {
		loginManager=new LoginManager();
		bar.setListener(null);
		bar.setTitle("找回密码");
		timer = new DownTimer(60 * 1000, 1000);
	}

	@Override
	public void addListeners() {
		getvcode.setOnClickListener(this);
		next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.forgetpsw_get_vcode:
			if (tel.getText().toString().length() == 11&&Tools.validatePhone(Tools.getTextViewText(tel))) {
				
				loadingDialog.show();
				loginManager.getVcodeOnFindPsw(Tools.getTextViewText(tel), "fp", getvCodeListener);
			} else {
				showToast("请输入正确的手机号");
			}
			break;
		case R.id.forgetpsw_next:
			if (tel.getText().toString().length() == 11
					&& psw.getText().toString().length() == 6) {

				loadingDialog.show();
				loginManager.checkVCode(Tools.getTextViewText(tel), Tools.getTextViewText(psw), checkVcodeListener);
				
			} else {
				showToast("请将信息补充完整");
			}
			break;

		default:
			break;
		}

	}

	/**
	 * 开启定时任务(主要是获取电话验证码)
	 */
	private final class DownTimer extends CountDownTimer {

		public DownTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			// 倒计时开始
			long m = millisUntilFinished / 1000;
			String ss = String.valueOf(m % 3600 % 60);
			ss = twoLength(ss);
			getvcode.setText(ss + " 秒");
		}

		/**
		 * 如果小于位，加0
		 * 
		 * @param str
		 * @return
		 */
		private String twoLength(String str) {
			if (str.length() == 1) {
				return str = "0" + str;
			}
			return str;
		}

		@Override
		public void onFinish() {
			getvcode.setClickable(true);
			getvcode.setText("重发");
		}
	}
	NListener<BaseData> getvCodeListener=new NListener<BaseData>() {

		@Override
		public void onResponse(BaseData data) {
			loadingDialog.dismiss();
			if (data.status==Constant.STATUS_OK) {
				//showToast("验证码"+data.data.vCode);
				getvcode.setText("59" + " 秒");
				getvcode.setClickable(false);
				timer.start();
				register(data.data.vCode);
			}else {
				showToast(data.msg);
			}
		}

		@Override
		public void onErrResponse(VolleyError error) {
			loadingDialog.dismiss();
			showToast(error.getMessage());
		}

		@Override
		public void onAllPageLoaded(int nowPage, int totalPage) {
			
		}
	};
	
	NListener<BaseData> checkVcodeListener=new NListener<BaseData>() {

		@Override
		public void onResponse(BaseData data) {
			loadingDialog.dismiss();
			if (data.status==Constant.STATUS_OK) {
				Intent intent=new Intent(mActivity, SetPasswordActivity.class);
				intent.putExtra("type", 0);
				intent.putExtra("tel", Tools.getTextViewText(tel));
				startActivity(intent);
			}else {
				showToast(data.msg);
			}
		}

		@Override
		public void onErrResponse(VolleyError error) {
			loadingDialog.dismiss();
			showToast(error.getMessage());
		}

		@Override
		public void onAllPageLoaded(int nowPage, int totalPage) {
			
		}
	};
	
	private SMSBroadcastReceiver receiver;

	private void register(String code) {
		receiver = new SMSBroadcastReceiver(code, new MessageListener() {
			@Override
			public void onReceived(String message) {
				psw.setText(message);
				unRegister();
			}
		});
		IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION);
		intentFilter.setPriority(Integer.MAX_VALUE);
		// 注册广播
		this.registerReceiver(receiver, intentFilter);
	}
	
	private void unRegister() {
		if(receiver != null)
			this.unregisterReceiver(receiver);
		receiver = null;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegister();
	}

}
